public class WorkersList {
    Worker[] _items;
    int _capacity;
    int _count;

    private void grow() {
        if (this._count >= this._capacity) {
            this._capacity = this._capacity * 2;
            Worker[] tmp = new Worker[this._capacity];
            for (int i = 0; i < this._count; i++)
                tmp[i] = this._items[i];
            this._items = tmp;
        }
    }

    public WorkersList(int capacity) {
        this._capacity = capacity;
        this._items = new Worker[capacity];
    }

    public int count() {
        return _count;
    }
    
    public Worker item(int index) {
        if (index < 0 || index >= this._count)
           return null;

        return this._items[index];
    }

    public void add(Worker worker) {
        grow();
        this._items[this._count] = worker;
        this._count++;
    }

    public void del(int index) {
        if (index < 0 || index >= this._count)
            return;

        this._items[index] = null;

        for (int i = index + 1; i < this._count; i++)
            this._items[i - 1] = this._items[i];
        this._count--;
    }

    private int compareStrings(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        int len = (len1 > len2) ? len1 : len2;
        
        for (int i = 0; i < len; i++) {
            if (s1.charAt(i) > s2.charAt(i))
                return 1;

            if (s1.charAt(i) < s2.charAt(i))
                return -1;
        }

        if (len1 > len2)
            return 1;

        if (len1 < len2)
            return -1;

        return 0;
    }

    public void sortByName() {
        for (int j = 1; j < this._count; j++)
            for (int i = 1; i < this._count; i++)
                if (compareStrings(this._items[i - 1].name, this._items[i].name) > 0) {
                    Worker tmp = this._items[i - 1];
                    this._items[i - 1] = this._items[i];
                    this._items[i] = tmp;
                }

    }

    public void sortBySalery() {
        for (int j = 1; j < this._count; j++)
            for (int i = 1; i < this._count; i++)
                if (this._items[i - 1].salary > this._items[i].salary) {
                    Worker tmp = this._items[i - 1];
                    this._items[i - 1] = this._items[i];
                    this._items[i] = tmp;
                }

    }
    
    public int totalSalery() {
        int sum = 0;
        for (int i = 0; i < this._count; i++)
            sum += this._items[i].salary;
        return sum;
    }
}
